package tw.com.chttl;

import java.io.BufferedOutputStream;
import java.io.IOException;  

import java.io.File;    
import java.io.FileOutputStream;   

import android.content.Context;  
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;  
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback;    
import android.hardware.Camera.ShutterCallback;  
import android.util.Log;  
import android.view.SurfaceHolder;  
import android.view.SurfaceView;  
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{  
    SurfaceHolder holder;  
    Camera myCamera;  
    
   
    private PictureCallback jpeg = new PictureCallback() {    
            
        public void onPictureTaken(byte[] data, Camera camera) {    
            // TODO Auto-generated method stub    
        	try    
            {    
                Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);    
                File file = new File("/sdcard/wjh.jpg");    
                BufferedOutputStream bos     
                = new BufferedOutputStream(new FileOutputStream(file));    
                bm.compress(Bitmap.CompressFormat.JPEG,100,bos);    
                bos.flush();    
                bos.close();    
            }catch(Exception e)    
            {    
                e.printStackTrace();    
            }      
                
        }    
    };    
    public MySurfaceView(Context context)  
    {  
        super(context);  
        holder = getHolder();
        holder.addCallback(this);  
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    }  
    public void tackPicture()    
    {    
        myCamera.takePicture(null,null,jpeg);    
    }    
    public void voerTack()    
    {    
        myCamera.startPreview();    
    }    
    
    public void surfaceChanged(SurfaceHolder holder, int format, int width,  
            int height) {  
    	  Camera.Parameters params = myCamera.getParameters();    
          params.setPictureFormat(PixelFormat.JPEG);    
          params.setPreviewSize(480,480);    
          myCamera.setParameters(params);    
          myCamera.startPreview();    
        myCamera.startPreview();          
    }  
   
    public void surfaceCreated(SurfaceHolder holder) {  
        // TODO Auto-generated method stub  
        if(myCamera == null)  
        {  
            myCamera = Camera.open(); 
            try {  
                myCamera.setPreviewDisplay(holder);  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }         
    }  
   
    public void surfaceDestroyed(SurfaceHolder holder) {  
        // TODO Auto-generated method stub  
        myCamera.stopPreview();
        myCamera.release();  
        myCamera = null;  
        Log.d("ddd", "4");    
    }  
}  
